package response

import (
	"fmt"
	"gitee.com/baizhige/go-server-tempalte/util/arrays"
	"gitee.com/baizhige/message-go"
	"gitee.com/baizhige/mongong-go"
	"go.mongodb.org/mongo-driver/bson"
)

// 响应相关业务代码

func init() {
	message.Listen[MongoInitPost](initRespCode)
}

func Success(language string, data any) Response {
	res := SuccessResponse.ToRes(language)
	res.Data = data
	return res
}

func initRespCode(t MongoInitPost) {
	res, err := mongong.Coll[RespCode]().Find(bson.M{})
	if err != nil {
		//todo 日志包输出
		fmt.Println("initRespCode", err)
		return
	}
	arr := arrays.ArrToArr(res, func(item *RespCode) RespCode {
		return *item
	})
	toMap := arrays.ArrToMap(arr, func(item RespCode) string {
		return item.PlatType + "_" + item.Code
	})
	RespMap = toMap
	fmt.Println("resp code init complete")
}
